package com.example.famhealing.lockscreen;

import android.app.Activity;
import android.app.KeyguardManager;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;

public class LockScreenService extends Service {
	private KeyguardManager km = null;
	private KeyguardManager.KeyguardLock keylock = null;

	private BroadcastReceiver mReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();
			if (action.equals("android.intent.action.SCREEN_OFF")) {
				Intent i = new Intent(context, LockScreenActivity.class);
				i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				context.startActivity(i);
			}
		}
	};

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}
	
	public void onCreate()
	{
		super.onCreate();
		
		km=(KeyguardManager)this.getSystemService(Activity.KEYGUARD_SERVICE);
		if(km!=null)
		{
			keylock=km.newKeyguardLock("test");
			keylock.disableKeyguard();
		}
	}
	
	public int onStartCommand(Intent intent, int flags, int startId)
	{
		IntentFilter filter=new IntentFilter("com.example.famhealing.action.isAlive");
		filter.addAction(Intent.ACTION_SCREEN_OFF);
		registerReceiver(mReceiver, filter);
		return Service.START_NOT_STICKY;
	}
	
	public void onDestroy()
	{
		if(keylock!=null)
			keylock.reenableKeyguard();
		if(mReceiver!=null)
			unregisterReceiver(mReceiver);
	}
}
